package practise.leetcode.q98;

import practise.leetcode.structure.TreeNode;

public class Solution {
    public boolean isValidBST(TreeNode root) {
        return isValidBST(root, null, null);
    }

    private boolean isValidBST(TreeNode root, Integer lower, Integer upper) {
        if (root == null) {
            return true;
        }

        if (lower != null && lower >= root.val) {
            return false;
        }

        if (upper != null && upper <= root.val) {
            return false;
        }

        return isValidBST(root.left, lower, root.val)
                && isValidBST(root.right, root.val, upper);
    }
}
